﻿/*
 * Copyright © 2020, Wuxi Safover Intelligent Technology Co., Ltd. All rights reserved.
 * 
 * 无锡安永智能科技有限公司版权所有，未经许可，不得用于商业用途
 * 
 * Visit: https://www.safover.com/ 
 */

using System;
using Safover.LidarCollector.Models;
using Safover.Lidar.DataContract;

namespace Safover.LidarCollector.Managers
{
    internal class ScanParamsBuilder
    {
        internal static PeriodicScanParams GetPeriodicScanParams(LidarConfig lidarConfig)
        {
            switch (lidarConfig.LidarWorkMode)
            {
                case LidarWorkMode.Fixed:
                    {
                        var horizontalAngle = lidarConfig.FixedHorizontalAngle;
                        var verticalAngle = lidarConfig.FixedVertialAngle;
                        var singleScanEndDelayDuration = lidarConfig.SingleScanEndDelayDuration;

                        return new PeriodicFixedScanParams(lidarConfig.LidarWorkMode, verticalAngle, horizontalAngle, singleScanEndDelayDuration);
                    }
                case LidarWorkMode.Underway:
                    {
                        var horizontalAngle = lidarConfig.UnderwayHorizontalAngle;
                        var verticalAngle = lidarConfig.UnderwayVertialAngle;
                        string dateTimeString = DateTime.Now.ToString("yyyyMMddHHmmss");
                        //string lastFolderNameForUnderwayMode = $"VEHICLE_{verticalAngle}_{horizontalAngle}_{dateTimeString}";
                        return new PeriodicUnderwayScanParams(lidarConfig.LidarWorkMode, verticalAngle, horizontalAngle/*, lastFolderNameForUnderwayMode*/);
                    }
                case LidarWorkMode.Horizontal:
                    {
                        var start = lidarConfig.HorizontalStartAngle;
                        var range = lidarConfig.HorizontalRangeAngle;
                        var step = lidarConfig.HorizontalStepAngle;
                        var fix = lidarConfig.HorizontalScanFixedVerticalAngle;
                        var horizontalBlindSectorConfigs = lidarConfig.HorizontalBlindSectorConfigs.ToArray();
                        var periodScanEndDelayDuration = lidarConfig.PeriodScanEndDelayDuration;

                        return new PeriodicHorizontalRangeScanParams(lidarConfig.LidarWorkMode, start, range, fix, step, horizontalBlindSectorConfigs, periodScanEndDelayDuration);
                    }
                case LidarWorkMode.Vertical:
                    {
                        var start = lidarConfig.VerticalStartAngle;
                        var range = lidarConfig.VerticalRangeAngle;
                        var step = lidarConfig.VerticalStepAngle;
                        var fix = lidarConfig.VerticalScanFixedHorizontalAngle;
                        var verticalBlindSectorConfigs = lidarConfig.VerticalBlindSectorConfigs.ToArray();
                        var periodScanEndDelayDuration = lidarConfig.PeriodScanEndDelayDuration;

                        return new PeriodicVerticalRangeScanParams(lidarConfig.LidarWorkMode, start, range, fix, step, verticalBlindSectorConfigs, periodScanEndDelayDuration);
                    }
                default:
                    return null;
            }
        }

        internal static PeriodicScanParams GetPeriodicScanParamsForMultiMode(SingleScanConfig scanStep, LidarConfig lidarConfig)
        {
            LidarWorkMode lidarWorkMode = LidarWorkMode.Multi;

            switch (scanStep.LidarWorkMode)
            {
                case LidarWorkMode.Fixed:
                    {
                        var configs = scanStep as FixedDirectionScanConfig;
                        var horizontalAngle = configs.HorizontalAngle;
                        var verticalAngle = configs.VertialAngle;
                        var singleScanEndDelayDuration = configs.SingleScanEndDelayDuration;

                        return new PeriodicFixedScanParams(lidarWorkMode, verticalAngle, horizontalAngle, singleScanEndDelayDuration);
                    }
                case LidarWorkMode.Horizontal:
                    {
                        var configs = scanStep as HorizontalScanConfig;
                        var start = configs.HorizontalStartAngle;
                        var range = configs.HorizontalRangeAngle;
                        var fix = configs.FixedVertialAngle;
                        var step = configs.HorizontalStepAngle;
                        var horizontalBlindSectorConfigs = lidarConfig.HorizontalBlindSectorConfigs.ToArray();
                        var periodScanEndDelayDuration = configs.PeriodScanEndDelayDuration;

                        return new PeriodicHorizontalRangeScanParams(lidarWorkMode, start, range, fix, step, horizontalBlindSectorConfigs, periodScanEndDelayDuration);
                    }
                case LidarWorkMode.Vertical:
                    {
                        var configs = scanStep as VerticalScanConfig;
                        var start = configs.VerticalStartAngle;
                        var range = configs.VerticalRangeAngle;
                        var fix = configs.FixedHorizontalAngle;
                        var step = configs.VerticalStepAngle;
                        var verticalBlindSectorConfigs = lidarConfig.VerticalBlindSectorConfigs.ToArray();
                        var periodScanEndDelayDuration = configs.PeriodScanEndDelayDuration;

                        return new PeriodicVerticalRangeScanParams(lidarWorkMode, start, range, fix, step, verticalBlindSectorConfigs, periodScanEndDelayDuration);
                    }
                default:
                    return null;
            }
        }
    }

}
